import com.csvreader.CsvReader;
import org.apache.poi.hssf.usermodel.*;

import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: 陈钦亚
 * @Date: 2019/6/4 17:33
 */
public class ExcelTool {
    private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    public static List<List<String>> read(InputStream in, String[] titles) {
        List<List<String>> lists = new ArrayList<>();
        try {
            // 创建CSV读对象
            CsvReader csvReader = new CsvReader(in, DEFAULT_CHARSET);
            List<String> phones = new ArrayList<>();
            Set<Integer> integers = new HashSet<>();
            // 读表头
            csvReader.readHeaders();
            while (csvReader.readRecord()) {
                // 读一整行
                List<String> strings = new ArrayList<>();
                boolean clean = true;
                for (String s : titles) {
                    StringBuilder stringBuilder = new StringBuilder();
                    String[] splits = s.split("#&");
                    if (splits.length > 0) {
                        for (String ss : splits) {
                            String temp = csvReader.get(ss);
                            if (ss.equals("手机") && clean) {
                                clean = false;
                                if (phones.contains(temp)) {
                                    integers.add(phones.indexOf(temp));
                                    integers.add(phones.size());
                                }
                                phones.add(temp);
                            }
                            stringBuilder.append(temp);
                        }
                    }
                    strings.add(stringBuilder.toString());
                }
                lists.add(strings);
            }
            System.out.println(lists.size() + "删除：" + integers.size());
            for (Integer integer : integers) {
                System.out.println("111：" + integer);
            }
            List<List<String>> tempList = new ArrayList<>();
            for (Integer integer : integers) {
                List<String> strings = lists.get(integer);
                tempList.add(strings);
                System.out.println(integers.size() + "删除：" + strings.get(2));
            }
            lists.removeAll(tempList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lists;
    }

    public static void saveHSSFWorkbook(File file, String sheetName, String[] title, List<List<String>> values) {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet(sheetName);
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //声明列对象
        HSSFCell cell = null;
        //创建标题
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }
        //创建内容
        for (int i = 0; i < values.size(); i++) {
            row = sheet.createRow(i + 1);
            List<String> strings = values.get(i);
            for (int j = 0; j < strings.size(); j++) {
                //将内容按顺序赋给对应的列对象
                row.createCell(j).setCellValue(strings.get(j));
            }
        }
        //  写文件
        try {
            FileOutputStream fos = null;
            fos = new FileOutputStream(file);
            wb.write(fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
